package leeon.forpeddy;

import ipworks.IPWorksException;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TooManyListenersException;

import leeon.forpeddy.download.HtmlDownloader;
import leeon.forpeddy.excel.ClassExcelWrapper;
import leeon.forpeddy.htmlparse.ClassHtmlParser;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ClassHelper {
	private static Log logger = LogFactory.getLog(ClassHelper.class);
	
	public static final String BASE_DIR = "D:\\project\\eurospace\\forpeddy\\misc\\";
	//public static final String HTTP_URL = "http://218.1.69.105/studenthistory/viewStudInfo.jsp?userid=gmyan&isDisplay=1";
	public static final String HTTP_URL = "http://218.1.69.105/studenthistory/moveStudInfoToHistory.jsp?userid=hbli&tag=1";
	//public static final String HTTP_URL = "http://218.1.69.105/studenthistory/viewStudInfo_gqb.jsp?userid=gmyan&isDisplay=1";
	
	private List<Map<String, String>> list = null;
	private List<Map<String, String>> result = null;
	
	public ClassHelper() {
		ClassExcelWrapper gew = null;
		try {
			gew = new ClassExcelWrapper(new File(BASE_DIR + "list.xls"));
			this.list = gew.readAll();
		} catch (Exception e) {
			logger.error("something wrong...", e);
		} finally {
			try {
				gew.save();
			} catch (Exception e) {
				logger.error("something wrong...", e);
			}
		}
		result = new ArrayList<Map<String, String>>();
	}
	
	
	public void doStartDownload() throws IPWorksException, TooManyListenersException, IOException {
		HtmlDownloader h = new HtmlDownloader();
		for (Map<String, String> row : this.list) {
			String fileName = row.get("time") + "_" + row.get("area") + "_" + row.get("name");
			File file = new File(BASE_DIR + fileName + ".html");
			if (!file.exists() || file.length() == 0) {
				h.start(HTTP_URL + "&major=" + row.get("id") + "&class0=" + row.get("cno"));
				h.saveToFile(file);
			} else {
				logger.info("download html for class[" + row.get("cno") + "] " + fileName + " file exists");
			}
		}
		h.destroy();
	}

	public void doOutput() {
		for (Map<String, String> r : this.result) {
//			String sql = "insert into peddy_studentlist2 (no, time, area, class, name) values ('" +
//					r.get("no") +
//					"', '" +
//					r.get("time") +
//					"', '" +
//					r.get("area") +
//					"', '" +
//					r.get("class") +
//					"', '" +
//					r.get("name") +
//					"');";
			String sql = "," + r.get("no") +
					"," +
					r.get("time") +
					"," +
					r.get("area") +
					"," +
					r.get("class") +
					"," +
					r.get("name");
			logger.info(sql);
		}
	}


	public void doParseHtml() throws IOException {
		for (Map<String, String> row : this.list) {
			String fileName = row.get("time") + "_" + row.get("area") + "_" + row.get("name");
			logger.info("parser html for class[" + row.get("cno") + "] " + fileName);
			ClassHtmlParser chp = new ClassHtmlParser(BASE_DIR + fileName + ".html");
			result.addAll(chp.getResults(row.get("time"), row.get("area"), row.get("name")));
		}
	}
	
	public void doClose() throws IOException, IPWorksException, TooManyListenersException {
		HtmlDownloader h = new HtmlDownloader();
		for (Map<String, String> row : this.list) {
			File file = new File(BASE_DIR + row.get("id") + ".html");
			if (!file.exists() || file.length() == 0) {
				h.start(HTTP_URL + "&R=" + row.get("id"));
				h.saveToFile(file);
			} else {
				logger.info("close no html for student[" + row.get("id") + "]  file exists");
			}
		}
		h.destroy();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ClassHelper ch = null;
		try {
			ch = new ClassHelper();
//			ch.doStartDownload();
//			ch.doParseHtml();
//			ch.doOutput();
			ch.doClose();
		} catch (Exception e) {
			logger.error("something wrong...", e);
		}

	}


}
